{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Lab 10 - Sampling distributions, Part 1\n", "\n", "For this lab, we will use a list of the top 1000 movies on [IMDb](https://www.imdb.com) compiled by [Kevin Markham](https://www.dataschool.io/about/) several years ago. \n", "\n", "The data CSV file is on GitHub here: [imdb_1000.csv](https://github.com/justmarkham/DAT8/blob/master/data/imdb_1000.csv) To download, right click on Raw and save the CSV file.\n", "\n", "### Iteration\n", "First we will look at iteration, which is a way to repeat a section of code multiple time without retyping it. Try running the code below. What does it do?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n", "Hello!\n" ] } ], "source": [ "for i in range(10):\n", " print(\"Hello!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This code is repeated below. Can you modify it to print `Good-bye!` 5 times?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for i in range(10):\n", " print(\"Hello!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What do you think the following code does? Make a guess and then run it." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lehman\n", "College\n", "Lehman\n", "College\n", "Lehman\n", "College\n", "Lehman\n", "College\n" ] } ], "source": [ "for i in range(4):\n", " print(\"Lehman\")\n", " print(\"College\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What do you think the following code does? Make a guess and then run it." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Starting...\n", "In the loop\n", "In the loop\n", "In the loop\n", "Ending...\n" ] } ], "source": [ "print(\"Starting...\")\n", "for i in range(3):\n", " print(\"In the loop\")\n", "print(\"Ending...\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Only the code that is indented is repeated. Another name for iteration is a *loop*.\n", "\n", "Can you write code that uses a loop to display the following?\n", "\n", "`This is my\n", "loop\n", "loop\n", "loop\n", "loop\n", "loop\n", "!`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you write code that uses a loop to display the following?\n", "\n", "`This is a\n", "fancier\n", "loop\n", "fancier\n", "loop\n", "fancier\n", "loop\n", "isn't\n", "it?`" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.6" } }, "nbformat": 4, "nbformat_minor": 2 }